package com.stu.student_back_end.controller;

import com.stu.student_back_end.common.Result;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblBuilding;
import com.stu.student_back_end.service.ITblBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 楼栋操作
 */

@RestController
public class BuildingController {

    @Autowired
    public ITblBuildingService buildingService;

    /**
     * 保存楼栋信息
     * @param building
     * @return
     */
    @PostMapping("/build/save")
    public @ResponseBody
    Result saveBuildingAndAccount(@RequestBody TblBuilding building) {
        return buildingService.saveBuildingAndAccount(building);
    }

    /**
     * 查询所有楼栋信息
     * @return
     */
    @GetMapping("/b/queryAll")
    public @ResponseBody
    Result queryAllBuilding() {
        List<TblBuilding> list = buildingService.list();

        if(list == null) return Result.success("查无数据！");
        //查询成功
        return Result.success("查询成功！",list);
    }

    /**
     * 分页查询楼栋
     * @param pageDTO
     * @return
     */
    @PostMapping("/b/query")
    public Result queryBuildingPage(@RequestBody PageDTO pageDTO) {
        return Result.success(buildingService.queryBuildingPage(pageDTO));
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @PostMapping("/b/byId/{id}")
    public Result queryByIdBuilding(@PathVariable("id") int id) {
        TblBuilding building = buildingService.getById(id);
        //判断是否为空
        if(building == null) return Result.success("查无数据！");

        //不为空
        return Result.success("查询成功！",building);
    }

    /**
     * 修改楼栋信息
     * @param tblBuilding
     * @return
     */
    @PostMapping("/b/update")
    public Result updateBuildingAndAccount(@RequestBody TblBuilding tblBuilding) {
        return buildingService.updateBuildingAndAccount(tblBuilding);
    }

    /**
     * 根据id删除楼栋信息
     * @param building
     * @return
     */
    @PostMapping("/b/del")
    public Result delByIdBuildingAndAccount(@RequestBody TblBuilding building) {
        return buildingService.delByIdBuildingAndAccount(building);
    }
}
